package org.linlinjava.litemall.wx.web;

import com.github.pagehelper.PageInfo;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallCommodityComment;
import org.linlinjava.litemall.db.domain.LitemallOrderGoods;
import org.linlinjava.litemall.db.service.LitemallCommodityCommentService;
import org.linlinjava.litemall.db.service.LitemallOrderGoodsService;
import org.linlinjava.litemall.db.util.BadWordUtil;
import org.linlinjava.litemall.db.util.OrderGoodsConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 打分.评价
 */
@RestController
@RequestMapping("wx/review")
public class WxReviewController {
    @Autowired
    LitemallCommodityCommentService service;
    @Autowired
    LitemallOrderGoodsService orderGoodsService;



    /**
     * commodityId 商品ID 获得 打分
     * @param msg
     * @return
     */
    @PostMapping("list")
    public Object list(@RequestBody String msg) {
        Integer page = JacksonUtil.parseInteger(msg, "page");
        Integer pageSize = JacksonUtil.parseInteger(msg, "pageSize");
        Integer commodityId = JacksonUtil.parseInteger(msg, "commodityId");
        if (page == null || pageSize == null || commodityId == null) {
            return ResponseUtil.badArgument();
        }
        if (page < 0 || pageSize < 0 || commodityId < 0) {
            return ResponseUtil.badArgumentValue();
        }
        PageInfo<LitemallCommodityComment> list = service.selectByCommodity(page, pageSize, commodityId);
        if (list != null && list.getTotal() > 0) {
            Map<String, Object> map = service.selectInfoByCommodity(list);
            return ResponseUtil.ok(map);
        }
        return ResponseUtil.ok();
    }

    @PostMapping("insert")
    public Object insert(@RequestBody String msg, HttpServletRequest request) {
/*        "id": 1,
                "orderCommodityId": 1,
                "customerId": 6,
                "review": 5,
                "createTime": "2019-11-19 18:52:11",
                "updateTime": "2019-11-19 18:52:11",
                "detail": "挺好的"*/

        Integer orderCommodityId = JacksonUtil.parseInteger(msg, "orderCommodityId");
        XxlSsoUser user = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        Long customerId = Long.parseLong(user.getUserid());
        String review = JacksonUtil.parseString(msg, "review");
        String detail = JacksonUtil.parseString(msg, "detail");
        List<String> detailPhoto = JacksonUtil.parseStringList(msg, "detailPhoto");
        if (orderCommodityId == null || review == null) {
            return ResponseUtil.badArgument();
        }
        LitemallOrderGoods goods = orderGoodsService.findById(orderCommodityId);
        if (goods == null || !goods.getState().equals(OrderGoodsConstant.COMPLETE)) {
            return ResponseUtil.fail(402, "订单不在合法状态");
        }
        Integer comment = goods.getComment();
        if (comment != 0) {
            return ResponseUtil.fail(402, "您已评论");
        }
        if (StringUtils.isNotEmpty(detail)) {
            detail = BadWordUtil.replaceBadWord(detail, 2, "**");
        }
        try {
            if (detailPhoto != null && detailPhoto.size() > 0) {
                String[] photos = new String[detailPhoto.size()];
                photos = detailPhoto.toArray(photos);
                service.insert(new LitemallCommodityComment(goods.getGoodsId(), customerId, new BigDecimal(review), detail, photos));
                goods.setComment(++comment);
                orderGoodsService.updateById(goods);
            }
            else {
                service.insert(new LitemallCommodityComment(goods.getGoodsId(), customerId, new BigDecimal(review), detail));
                goods.setComment(++comment);
                orderGoodsService.updateById(goods);
            }
            return ResponseUtil.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(500, e.toString());
        }
    }
}
